package com.heal.error;

import lombok.Data;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

@Data
public class ErrorInfo {

    private Long timestamp;
    private Integer errorCode;
    private String message;

    public ErrorInfo() {
        this.timestamp = System.currentTimeMillis();
    }

    /**
     * 抛出异常信息.
     *
     * @param errorCode errorCode
     * @param message message
     * @return
     */
    public static ResponseEntity<Object> response(Integer errorCode, String message) {
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.setErrorCode(errorCode);
        errorInfo.setMessage(message);
        return new ResponseEntity<>(errorInfo, HttpStatus.INTERNAL_SERVER_ERROR);
    }

    /**
     * 设置状态码并抛出异常信息.
     *
     * @param errorCode errorCode
     * @param message message
     * @param httpStatus httpStatus
     * @return
     */
    public static ResponseEntity<Object> response(
            Integer errorCode, String message, HttpStatus httpStatus) {
        ErrorInfo errorInfo = new ErrorInfo();
        errorInfo.setErrorCode(errorCode);
        errorInfo.setMessage(message);
        return new ResponseEntity<>(errorInfo, httpStatus);
    }
}
